package;

import nme.display.Sprite;
import nme.Lib;
import nme.display.StageDisplayState;
import nme.events.Event;

/**
 * @author jonathanmoriarty
 */
 
//TODO: Investigate Fullscreen setting.
//TODO: Globalized object arrays to allow for more seperated update code.
//TODO: Reuse screens, Screen interface with hide, display, clean, reset.
//TODO: Import cleaned up static collision test class.
class THF extends Sprite 
{	
	public static var menuScreen:MenuScreen;
	public static var selectScreen:SelectScreen;
	public static var playScreen:PlayScreen;
	public static var creditsScreen:CreditsScreen;
	
	public static var STAGE_WIDTH:Float = 720.0;
	public static var STAGE_HEIGHT:Float = 405.0;
	
	private static var _gameMain:THF;
	public static var _input:InputManager;
	
	public function new()
	{
		super();
		_input = new InputManager( this );
		
		onRequestMenu();
		Lib.current.stage.addEventListener( Event.ENTER_FRAME, update );
	}
	
	public function clearScreens():Void
	{
		clearMenu();
		clearCredits();
		clearSelect();
		clearPlay();
	}
	
	public function clearMenu():Void
	{
		if ( menuScreen != null )
		{
			menuScreen.reset();
			menuScreen.hide();
			removeChild( menuScreen );
			menuScreen.removeEventListener( NavigationEvent.SELECT, onRequestSelect );
			menuScreen.removeEventListener( NavigationEvent.CREDITS, onRequestCredits );
			menuScreen = null;
		}
	}
	
	public function clearCredits():Void
	{
		if ( creditsScreen != null )
		{
			creditsScreen.reset();
			creditsScreen.hide();
			removeChild( creditsScreen );
			creditsScreen.removeEventListener( NavigationEvent.MENU, onRequestMenu );
			creditsScreen = null;
		}
	}
	
	public function clearSelect():Void
	{
		if ( selectScreen != null )
		{
			selectScreen.reset();
			selectScreen.hide();
			removeChild( selectScreen );
			selectScreen.removeEventListener( NavigationEvent.MENU, onRequestMenu );
			selectScreen.removeEventListener( NavigationEvent.PLAY, onRequestPlay );
			selectScreen = null;
		}
	}
	
	public function clearPlay():Void
	{
		if ( playScreen != null )
		{
			playScreen.reset();
			playScreen.hide();
			removeChild( playScreen );
			playScreen.removeEventListener( NavigationEvent.SELECT, onRequestSelect );
			playScreen.removeEventListener( NavigationEvent.PLAY, onRequestPlay );
			playScreen.removeEventListener( NavigationEvent.MENU, onRequestMenu );
			playScreen = null;
		}
	}
	
	public function onRequestMenu( l_navEvent:NavigationEvent = null ):Void
	{
		clearScreens();
		Lib.current.stage.stageFocusRect = false;
		menuScreen = new MenuScreen();
		menuScreen.addEventListener( NavigationEvent.SELECT, onRequestSelect );
		menuScreen.addEventListener( NavigationEvent.CREDITS, onRequestCredits );
		menuScreen.x = 0;
		menuScreen.y = 0;
		addChild( menuScreen );
		Lib.current.stage.focus = menuScreen;
	}
	
	public function onRequestPlay( l_navEvent:NavigationEvent = null ):Void
	{
		var l_character0:Int = selectScreen._selected0;
		var l_character1:Int = selectScreen._selected1;
	
		clearScreens();
		
		Lib.current.stage.stageFocusRect = false;
		playScreen = new PlayScreen( l_character0, l_character1 );
		playScreen.addEventListener( NavigationEvent.SELECT, onRequestSelect );
		playScreen.addEventListener( NavigationEvent.PLAY, onRequestPlay );
		playScreen.addEventListener( NavigationEvent.MENU, onRequestMenu );
		playScreen.x = 0;
		playScreen.y = 0;
		addChild( playScreen );
		Lib.current.stage.focus = playScreen;
	}
	
	public function onRequestSelect( l_navEvent:NavigationEvent = null ):Void
	{
		clearScreens();
		Lib.current.stage.stageFocusRect = false;
		selectScreen = new SelectScreen();
		selectScreen.addEventListener( NavigationEvent.PLAY, onRequestPlay );
		selectScreen.addEventListener( NavigationEvent.MENU, onRequestMenu );
		selectScreen.x = 0;
		selectScreen.y = 0;
		addChild( selectScreen );
		Lib.current.stage.focus = selectScreen;
	}
	
	public function onRequestCredits( l_navEvent:NavigationEvent = null ):Void
	{
		clearScreens();
		Lib.current.stage.stageFocusRect = false;
		creditsScreen = new CreditsScreen();
		creditsScreen.addEventListener( NavigationEvent.MENU, onRequestMenu );
		creditsScreen.x = 0;
		creditsScreen.y = 0;
		addChild( creditsScreen );
		Lib.current.stage.focus = creditsScreen;
	}
	
	public function update( l_event:Event ):Void 
	{
		if ( playScreen != null )
		{
			playScreen.update( l_event );
		}
		if ( menuScreen != null )
		{
			menuScreen.update( l_event );
		}
		if ( selectScreen != null )
		{
			selectScreen.update( l_event );
		}
		InputManager.update();
	}
	
	public static function main() 
	{
		_gameMain = new THF();
		
		// Taken from forum post on NME forums.
		/*Lib.current.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
		//trace(Lib.current.stage.stageWidth);
		var xScaleFresh:Int = Math.floor(cast(Lib.current.stage.stageWidth, Float) / cast(1024, Float));
		var yScaleFresh:Int = Math.floor(cast(Lib.current.stage.stageHeight, Float) / cast(768, Float));
		if (xScaleFresh < yScaleFresh)
		{
			_gameMain.scaleX = cast(xScaleFresh, Float);
			_gameMain.scaleY = cast(xScaleFresh, Float);
		}
		else if (yScaleFresh < xScaleFresh)
		{
			_gameMain.scaleX = cast(yScaleFresh, Float);
			_gameMain.scaleY = cast(yScaleFresh, Float);
		} 
		else 
		{
			_gameMain.scaleX = cast(xScaleFresh, Float);
			_gameMain.scaleY = cast(yScaleFresh, Float);
		}*/
		//_gameMain.x = (cast(Lib.current.stage.stageWidth, Float) / 2.0) - (_gameMain.width / 2.0);
		//_gameMain.y = (cast(Lib.current.stage.stageHeight, Float) / 2.0) - (_gameMain.width / 2.0);
	
	
		Lib.current.addChild( _gameMain );
		//trace(xScaleFresh);
		//trace(yScaleFresh);
	}
}